package com.egao.common.module.guidance.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.guidance.entity.LectureApply;
import com.egao.common.module.guidance.entity.vo.LectureApplyVO;
import com.egao.common.module.guidance.service.LectureApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 就业讲座报名管理控制层
 *
 * @author pyx
 */
@Api(tags = "就业讲座报名管理")
@RestController
@RequestMapping("${api}")
public class LectureApplyController extends BaseController {
    @Autowired
    private LectureApplyService lectureApplyService;

    @PreAuthorize("hasAuthority('lecture:apply:list')")
    @ApiOperation("就业讲座报名信息")
    @OperLog(value = "就业讲座报名管理", desc = "查询就业讲座报名信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "讲座id", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "审核状态", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/guidance/lectureApply/get")
    public JsonResult listGetLecture(Integer id, Integer state) {
        return JsonResult.ok().setData(lectureApplyService.listGetLecture(id, state));
    }

    @PreAuthorize("hasAuthority('lecture:apply:update')")
    @ApiOperation("审核报名讲座")
    @OperLog(value = "就业讲座报名管理", desc = "审核报名讲座", result = true)
    @PutMapping("/guidance/lectureApply/update")
    public JsonResult updateLectureApply(@RequestBody LectureApply lectureApply) {
        return lectureApplyService.updateLectureApply(lectureApply);
    }

    @PreAuthorize("hasAuthority('lecture:apply:insert')")
    @ApiOperation("讲座报名")
    @OperLog(value = "就业讲座报名管理", desc = "报名讲座", result = true)
    @PostMapping("/guidance/lectureApply/insert")
    public JsonResult insertLectureApply(Integer lectureId) {
        if (getLoginUserId() == null) {
            return JsonResult.error("请先登录");
        }

        return lectureApplyService.insertLectureApply(lectureId, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('lecture:apply:update')")
    @ApiOperation("取消报名")
    @OperLog(value = "就业讲座报名管理", desc = "取消报名", result = true)
    @GetMapping("/guidance/lectureApply/cancel")
    public JsonResult cancelLectureApply(Integer lectureId) {
        if (getLoginUserId() == null) {
            return JsonResult.error("请先登录");
        }

        return lectureApplyService.cancelApply(lectureId, getLoginUserId());
    }


    @PreAuthorize("hasAuthority('student')")
    @ApiOperation("报名讲座信息")
    @OperLog(value = "报名讲座管理", desc = "分页查询讲座信息")
    @GetMapping("/guidance/lectureApply/list")
    public PageResult<LectureApplyVO> page(HttpServletRequest request) {
        PageParam<LectureApplyVO> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(lectureApplyService.listApplyPage(pageParam, getLoginUserId()), pageParam.getTotal());
    }
}
